package com.config.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.config.common.Result;
import com.config.entity.Instrument;
import com.config.service.InstrumentService;
import com.config.vo.InstrumentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 试剂警告前端控制器
 * </p>
 *
 * @author Lu
 * @since 2021-02-04
 */
@Api (tags ="试剂警告设置页面接口")
@RestController
@RequestMapping ("/config/instrument")
public class InstrumentController {

	@Autowired
	InstrumentService instrumentService;

	@Cacheable ()
	@ApiOperation ("获取所有信息")
	@GetMapping ("/instrumentAll")
	public Result selectAll() {
		QueryWrapper<Instrument> queryWrapper = new QueryWrapper<>();
		List<Instrument> list = instrumentService.list(queryWrapper);
		return Result.succ(list);
	}

	@ApiOperation ("分页模糊查询")
	@GetMapping ("/instrumentLikeAll")
	public Result selectLikeAll(Integer current, Integer size,String name) {
		//拼接sql
		QueryWrapper<Instrument> queryWrapper = new QueryWrapper<>();
		queryWrapper.like("in_id",name);
		queryWrapper.or().like("in_name", name);
		queryWrapper.or().like("it_name", name);
		//分页
		InstrumentVo userVo = new InstrumentVo();
		IPage<Instrument> page = new Page<>(current, size);
		userVo.setCurrent(current);
		userVo.setSize(size);
		userVo.setTotal(page.getTotal());
		userVo.setInstList(page.getRecords());
		IPage<Instrument> page1 = instrumentService.page(page, queryWrapper);
		return Result.succ(page1);
	}

	@ApiOperation ("获取所有信息（分页）")
	@GetMapping ("/instrumentPageAll")
	public Result selectpageAll(Integer current, Integer size) {
		InstrumentVo userVo = new InstrumentVo();
		IPage<Instrument> page = new Page<>(current, size);
		IPage<Instrument> page1 = instrumentService.page(page, null);
		userVo.setCurrent(current);
		userVo.setSize(size);
		userVo.setTotal(page.getTotal());
		userVo.setInstList(page.getRecords());
		return Result.succ(page1);
	}

	@ApiOperation ("修改列表数据")
	@PutMapping ("/inupd")
	public Result upinChoose0(@RequestBody Instrument instrument) {
		boolean update = instrumentService.updateById(instrument);
		if (update) {
			return Result.succ(instrument);
		} else {
			return Result.fail("修改失败");
		}
	}

	@ApiOperation ("添加列表数据")
	@PostMapping ("/save")
	public Result add(@RequestBody Instrument instrument) {
		boolean save = instrumentService.save(instrument);
		if (save) {
			return Result.succ(instrument);
		} else {
			return Result.fail("添加失败");
		}
	}

	@ApiOperation ("根据id删除列表数据")
	@DeleteMapping ("/deleteById")
	public Result delectByid(Integer inid) {
		boolean b = instrumentService.removeById(inid);
		if (b) {
			return Result.succ(inid);
		} else {
			return Result.fail("添加失败");
		}
	}


}

